package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderAnswer;
import com.meida.module.bus.client.enums.AnswerStateEnum;
import com.meida.module.bus.client.enums.OrderTypeEnum;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台订单答案列表
 */
@Component
public class BusOrderAnswerPageListHandler implements PageInterceptor<BusOrderAnswer> {


    @Override
    public void prepare(CriteriaQuery<BusOrderAnswer> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String userName = params.get("userName");
        String nickName = params.get("nickName");
        String mobile = params.get("mobile");
        String answerUserName = params.get("answerUserName");
        String answerUserMobile = params.get("answerUserMobile");
        cq.select(BusOrderAnswer.class, "answerId", "orderId", "answerState", "createTime");
        cq.select(BusOrder.class, "orderNo", "orderType");
        cq.addSelect("answerUser.userName answerUserName", "answerUser.mobile answerUserMobile");
        cq.addSelect("appUser.userName appUserUserName", "appUser.nickName appUserNickName", "appUser.mobile appUserUserMobile");
        cq.eq(BusOrderAnswer.class, "answerState");
        cq.ne(true, "answerState", -1);
        cq.eq(BusOrderAnswer.class, "isAnswer", 1);
        cq.eq(BusOrderAnswer.class, "orderId");
        cq.eq(BusOrder.class, "orderType");
        cq.ne("orderType", 2);
        cq.like(BusOrder.class, "orderNo");
        cq.like("appUser.userName", userName);
        cq.like("appUser.nickName", nickName);
        cq.like("appUser.mobile", mobile);
        cq.like("answerUser.userName", answerUserName);
        cq.like("answerUser.mobile", answerUserMobile);
        cq.ge("DATE_FORMAT(orderAnswer.createTime,'%Y-%m-%d')", beginDate);
        cq.le("DATE_FORMAT(orderAnswer.createTime,'%Y-%m-%d')", endDate);
        cq.createJoin(BusOrder.class).setMainField("orderId").setJoinField("orderId");
        cq.createJoin(AppUser.class).setJoinAlias("answerUser").setMainField("answerUserId").setJoinField("userId");
        cq.createJoin(AppUser.class).setJoinAlias("appUser").setMainAlias("busOrder").setMainField("userId").setJoinField("userId");
    }

    @Override
    public void complete(CriteriaQuery<BusOrderAnswer> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            Integer answerState = entityMap.getInt("answerState");
            AnswerStateEnum answerStateEnum = AnswerStateEnum.getByValue(answerState);
            entityMap.put("answerStateText", "");
            if (FlymeUtils.isNotEmpty(answerStateEnum)) {
                entityMap.put("answerStateText", answerStateEnum.getLabel());
            }
            entityMap.put("orderTypeText", "");
            Integer orderType = entityMap.getInt("orderType");
            OrderTypeEnum orderTypeEnum = OrderTypeEnum.getByValue(orderType);
            if (FlymeUtils.isNotEmpty(orderTypeEnum)) {
                entityMap.put("orderTypeText", orderTypeEnum.getLabel());
            }
        });
    }

}
